<?php

error_reporting(0); // Set E_ALL for debuging
function pr($c){
    echo '<pre>' . print_r($c , true) .'</pre>';
}
if(!defined('DS')){
    define('DS', DIRECTORY_SEPARATOR);
}

include_once dirname(__FILE__).DS.'elFinderConnector.class.php';
include_once dirname(__FILE__).DS.'elFinder.class.php';
include_once dirname(__FILE__).DS.'elFinderVolumeDriver.class.php';
include_once dirname(__FILE__).DS.'elFinderVolumeLocalFileSystem.class.php';
// Required for MySQL storage connector
// include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeMySQL.class.php';
// Required for FTP connector support
// include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeFTP.class.php';


/**
 * Simple function to demonstrate how to control file access using "accessControl" callback.
 * This method will disable accessing files/folders starting from  '.' (dot)
 *
 * @param  string  $attr  attribute name (read|write|locked|hidden)
 * @param  string  $path  file path relative to volume root directory started with directory separator
 * @return bool|null
 **/
function access($attr, $path, $data, $volume) {
	return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
		? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
		:  null;                                    // else elFinder decide it itself
}
/**
 * Simple function to validate file name
 *
 * @param  string  $name  file name (read|write|locked|hidden)
 * 
 * @return void
 **/
function checkValidName($name) {
    return preg_match('/^\w[\w\s\.\%\-]*$/u', $name) && (strpos($name, '.') === false
            || preg_match('/\.(gif|png|jpg|jpeg|xml|xls?x|doc|pdf|doc?x|ppt?x|rar|zip)$/i', $name));
    //return preg_match('/^\w[\w\s\.\%\-]*$/u', $name) && !preg_match('/\.(php|php4|htaccess|py|asp?x|cgi|conf|ini|zip|rar|7z|bz?2|gz|tgz|taz)$/i', $name);
}

$opts = array(
	// 'debug' => true,
	'roots' => array(
		array(
			'driver'        => 'LocalFileSystem',   // driver for accessing file system (REQUIRED)
			'path'          => dirname(dirname(dirname(dirname(__FILE__)))) .DS .'img'. DS . 'uploads',         // path to files (REQUIRED)
                        'alias' => 'Home',
                        'acceptedName' => 'checkValidName',
                        'uploadOverwrite' => false,
			'URL'           => '/img/uploads/', // URL to files (REQUIRED)
			'accessControl' => 'access'             // disable and hide dot starting files (OPTIONAL)
		)
	)
);
// run elFinder
$connector = new elFinderConnector(new elFinder($opts));
$connector->run();